<?php

class admin{

	function __construct(){
	
	}
	
	public function admins(){
		$query;
		$result = array();
		$res = array();
		$query = mysql_query("SELECT id FROM user WHERE  privilege='3'") or die (mysql_error);
		while ($res = mysql_fetch_array($query)){
			$result[] = array('id'=>$res['id']);
		}
		return $result;
	}
	
	public function show_pages(){
		$query;
		$result = array();
		$query = mysql_query("SELECT * FROM pages  ORDER BY title") or die (mysql_error());
		if ($query){
			$html = '<table class="users" id="all-pages-table">';
			$html .= '<thead><tr>
						<td>Название страницы</td>
						<td width="100">Создана</td>
						<td width="100">Редактирована</td>
						<td></td>					
					 </tr></thead><tbody>';
			while ($result = mysql_fetch_array($query)){
				$html.='<tr><td><a class="show_page" href="profile.php?action=update_page&id_page='.$result['id'].'">'.$result['title'].'</a></td>';				
				$html.='<td>'.date('d.m.Y H:i',$result['creation_date']).'</td>';
				$html.='<td>'.date('d.m.Y H:i',$result['change_date']).'</td>';
				if ($result['in_menu'])
					$html.='<td width="45"><input id="'.$result['id'].'" type="button" title="Удалить страницу с меню" class="in_menu_button"/>';
				else
					$html.='<td width="45"><input id="'.$result['id'].'" type="button" title="Добавить страницу в меню" class="no_in_menu_button"/>';
				$html.='<input id="'.$result['id'].'" type="button" title="Удалить" class="delete_page"/></td></tr>';
			}
			$html.='</tbody></table>';
			return $html;
		}
		return false;
	}
	
	public function find_pages($title){
	$title = '%'.$title.'%';
	$query;
	$result = array();
	$query = mysql_query("SELECT * FROM pages WHERE title LIKE '".$title."' ORDER BY title") or die (mysql_error());
	if ($query){
		$html = '<table class="pages" id="all-users-table">';
		$html .= '<thead>
					<tr>
					<td>Название страницы</td>
					<td>Создана</td>
					<td>Редактирована</td>
					<td></td>
					<td></td>						
					</tr>
				  <thead>';
		while ($result = mysql_fetch_array($query)){
			$html.='<tr><td><a class="show_page" href="profile.php?action=update_page&id_page='.$result['id'].'">'.$result['title'].'</a></td>';
			$html.='<td>'.date('j.m.y',$result['creation_date']).'</td>';
			$html.='<td>'.date('j.m.y',$result['change_date']).'</td>';
			if ($result['in_menu'])
				$html.='<td><input type="button" title="Добавить страницу в меню" class="in_menu_button"/></td>';
			else
				$html.='<td><input type="button" title="Добавить страницу в меню" class="no_in_menu_button"/></td>';
			$html.='<td><input type="button" title="Удалить" class="delete_page"/></td></tr>';
		}
		$html.='</table>';
		return $html;
	}
	return false;
	}
	
	public function about_user($id){
		$query;
		$user = array();
		$query = mysql_query("SELECT * FROM user WHERE id='".$id."'") or die(mysql_error());
		if (($query) && (mysql_num_rows($query)>0)){
			while ($result = mysql_fetch_array($query))	{
				$user[] = array('login'=>$result['login']);				
			}
			return $user;
		}
		return false;	
	}
	
	public function show_gamers(){
		$query;
		$result = array();
		$parent = array();
		$query = mysql_query("SELECT gamers.*,user.* FROM user RIGHT OUTER JOIN gamers ON user.id=gamers.UserChild WHERE UserChild!=0  ORDER BY user.ID DESC") or die (mysql_error());
		$html = '<table class="users" id="all-users-table">';
		$html .= '<thead>
				<tr>
					<td>ID</td>
					<td>ФИО</td>
					<td>Уровень</td>
					<td width="110">Последний визит</td>
					<td>Куратор</td>
					<td width="45"></td>
				</tr></thead><tbody>';
		while ($result = mysql_fetch_array($query)){
			$login = '';
			if ($result['next_parent']!=null){
				$parent = $this->about_user($result['next_parent']);
				if (!empty($parent))
					foreach($parent as $Iterator){
						$login = $Iterator['login'];
				}
			}
			
			$html.='<tr><td>'.$result['id'].'</td>';
			$html.='<td><a class="show_users" href="profile.php?action=update_user&id_user='.$result['id'].'">'.$result['login'].'</a></td>';
			$html.='<td>'.$result['layer'].'</td>';
			$html.='<td class="aligicenter">'.date('d.m.Y H:i',$result['registered']).'</td>';
			$html.='<td><a class="show_users" href="profile.php?action=update_user&id_user='.$result['next_parent'].'">'.$login.'</a></td>';
			$html.='<td>';
			if($_SESSION['user']['id'] != $result['id'])$html.='<input type="button" class="delete_user"/><a id="'.$result['id'].'" class="message_admin_user"></a>';
			$html.='</td></tr>';
		}
			//echo 
		$html .= '</tbody></table>';
		return $html;
	}
	
	public function show_users(){
		$parent = array();
		$login = '';
		$query;
		$result = array();
		//$query = mysql_query("SELECT gamers.*,user.* FROM user INNER JOIN gamers ON user.id=gamers.UserChild  ORDER BY login") or die (mysql_error());
		$query = mysql_query("SELECT gamers.*,user.* FROM user LEFT OUTER JOIN gamers ON user.id=gamers.UserChild  ORDER BY user.login") or die (mysql_error());
		$html = '<table class="users" id="all-users-table">';
		$html .= '<thead>
				<tr>
					<td>ID</td>
					<td>ФИО</td>
					<td>Уровень</td>
					<td>Последний визит</td>
					<td>Куратор</td>
					<td width="45"></td>
				</tr></thead><tbody>';
		while ($result = mysql_fetch_array($query)){
			$login = '';
			if ($result['next_parent']!=null){
				$parent = $this->about_user($result['next_parent']);
				if (!empty($parent))
					foreach($parent as $Iterator){
						$login = $Iterator['login'];
				}
			}
			
			$html.='<tr><td>'.$result['id'].'</td>';
			$html.='<td><a class="show_users" href="profile.php?action=update_user&id_user='.$result['id'].'">'.$result['login'].'</a></td>';
			$html.='<td>'.$result['layer'].'</td>';
			$html.='<td class="aligicenter">'.date('d.m.Y H:i',$result['registered']).'</td>';
			$html.='<td><a class="show_users" href="profile.php?action=update_user&id_user='.$result['next_parent'].'">'.$login.'</a></td>';
			$html.='<td>';
			if($_SESSION['user']['id'] != $result['id'])$html.='<input type="button" class="delete_user"/><a id="'.$result['id'].'" class="message_admin_user"></a>';
			$html.='</td></tr>';
		}
			//echo 
		$html .= '</tbody></table>';
		return $html;
	}
	
	public function show_free_users(){
		$query;
		$result = array();
		$query = mysql_query("SELECT id,login FROM user WHERE (id in (SELECT UserChild FROM gamers WHERE((number<3) and (payd=1)))) ORDER BY user.login") or die (mysql_error());

		$html='<table class="users">';
		while ($result = mysql_fetch_array($query)){
			$html.='<tr><td><a class="show_users" href="profile.php?action=update_user&id_user='.$result['id'].'">'.$result['login'].'</a>';
			$html.='<input type="button" class="delete_user"/>';
			$html.='<a id="'.$result['id'].'" class="message_admin_user"></a></td></tr>';
		}
		$html.='</table>';
		return $html;
	}	
	
	public function find_users($find){
		$find = '%'.$find.'%';
		$i = 0;
		$query;
		$result = array();
		$query = mysql_query("SELECT * FROM gamers,user WHERE user.privilege<3 and (user.id LIKE '".$find."' or user.login LIKE '".$find."' or user.name LIKE '".$find."' or user.surname LIKE '".$find."') and user.id=gamers.UserChild ORDER BY login") or die (mysql_error());
		$html = '<table class="users">';
		$html .= '<tr>
					<td>ФИО</td>
					<td>Регистрация</td>
					<td>Активность</td>
				</tr>';
		while ($result = mysql_fetch_array($query)){
			$html.='<tr><td><a class="show_users" href="profile.php?action=update_user&id_user='.$result['id'].'">'.$result['login'].'</a></td>';
			$html.='<td>Уровень'.$result['layer'].'</td>';
			$html.='<td>'.date('H:m, j-m-y',$result['registered']).'</td>';
			$html.='<td><input type="button" class="delete_user"/></td>';
			$html.='<td><a id="'.$result['id'].'" class="message_admin_user"></a></td></tr>';
			$i;
		}
		$html.='</table>';
		return $html;
	}
	
	public function parent_for_user(){
		$query;
		$result = array();
		$query = mysql_query("SELECT * FROM user WHERE privilege<3 ORDER BY login") or die (mysql_error());
		$html='<TABLE>';
		while ($result = mysql_fetch_array($query)){
			$html.='<tr><td><a class="show_page" href="profile.php?action=parent_user&id_user='.$result['id'].'">'.$result['login'].'</a>';
			$html.='<input type="button" class="delete_user"/></td></tr>';
		}
		$html.='</TABLE>';
		return $html;
	}
	
	public function select_pages($id){
		$id=(int)$id;
		$query;
		$respond = array();
		$query = mysql_query("SELECT * FROM pages WHERE id='".$id."'") or die (mysql_error());
		$i = 0;
		while ($result = mysql_fetch_array($query)){
			$respond[$i]['id'] = $result['id'];
			$respond[$i]['title'] = $result['title'];
			$respond[$i]['content'] = $result['content'];
			$respond[$i]['in_menu'] = $result['in_menu'];
			$respond[$i]['author'] = $result['author'];
			$respond[$i]['creation_date'] = $result['creation_date'];
			$respond[$i]['change_date'] = $result['change_date'];
			$respond[$i]['seo_title'] = $result['seo_title'];
			$respond[$i]['seo_description'] = $result['seo_description'];
			$respond[$i]['seo_keywords'] = $result['seo_keywords'];
			$i++;
		}
		
		return $respond;
	}
	
	public function select_idpage($title){
		$result = array();
		$query;
		$query = mysql_query("SELECT id FROM pages WHERE title = '".$title."'") or die (mysql_error());
		$result = mysql_fetch_array($query);
		return $result['id'];
	}
	
	public function delete_page($id){
		$query;
		if (mysql_query("DELETE FROM pages WHERE id = '".(int)$id."'"))			
			return true;
		return false;
	}
	
	public function update_page($data){
		$query;
		if(mysql_query("UPDATE pages SET title = '".$data['title']."',content = '".$data['content']."',in_menu = '".$data['in_menu']."',change_date = '".time()."', seo_title = '".$data['seo_title']."', seo_description = '".$data['seo_description']."', seo_keywords = '".$data['seo_keywords']."' WHERE id ='".$data['id']."'"))
			return true;
		return false;
	}
	
	public function upd_in_menu($id,$val){
		if(mysql_query("UPDATE pages SET `in_menu` = '".$val."' WHERE id ='".$id."';"))
			return true;
		return false;
	}
	
	public function add_page($data){
		$query;
		$result = array();
		$query = mysql_query("SELECT title FROM pages WHERE title = '".$data['title']."'") or die (mysql_error());
		$result = mysql_num_rows($query);
		if ($result == 0){
			if(mysql_query("INSERT INTO pages (title ,content ,in_menu ,author ,creation_date,change_date, seo_title, seo_description, seo_keywords) VALUES ('".$data['title']."', '".$data['content']."', '".$data['in_menu']."', '".$data['author']."', '".$data['creation_date']."','".time()."', '".$data['seo_title']."', '".$data['seo_description']."', '".$data['seo_keywords']."')"))
				return true;
			return false;
		}
		else
			return false;
	}
	
	public function change_privilege($id,$privilege){
		if(mysql_query("UPDATE user SET privilege = '".$privilege."' WHERE id ='".$id."'")) 
			return true;
		return false;
	}
	
	public function delete_user($id){
		if (mysql_query("DELETE FROM user WHERE id = '".(int)$id."'"))			
			return true;
		return false;
	}
	
	public function select_user($id){
		$result = array();
		$query;
		$query = mysql_query("SELECT * FROM user WHERE id='".$id."'") or die (mysql_error());
		$result = mysql_fetch_array($query);
		return $result;
	}
	
	public function update_user($data){
		$query;
		if ($query = mysql_query("UPDATE user SET name = '".$data['name']."',surname = '".$data['surname']."',phone = '".$data['phone']."',country = '".$data['country']."',city = '".$data['city']."',question = '".$data['question']."',response = '".$data['response']."', privilege = '".$data['privilege']."' WHERE id = '".$data['id']."'"))
			return true;
		return false;
	}
	
	public function admin_update_user($data){
		$query;
		if ($query = mysql_query("UPDATE user SET name = '".$data['name']."',surname = '".$data['surname']."',phone = '".$data['phone']."',email = '".$data['email']."',country = '".$data['country']."',city = '".$data['city']."', privilege = '".$data['privilege']."' WHERE id = '".$data['id']."'"))
			return true;
		return false;
	}

	public function TemplateAddUser($data){
		$query;
		$salt = $this->generate_salt();
		$date = time();
		$password = md5(md5($data['password']).md5($salt));
		$result = "INSERT INTO temp_user (
			login,password,salt,name,surname,phone,email,country,city,question,response,registered,confirm,privilege)
			VALUES ('".$data['login']."','".$password."','".$salt."','".$data['name']."','".$data['surname']."','".$data['phone']."','".$data['email']."','".$data['country']."', '".$data['city']."','".$data['question']."','".$data['response']."','".$date."','".$data['confirm_reg']."','".$data['privilege']."')";
		$query=mysql_query($result) or die(mysql_error());
		if ($query) return true;				
		return false;
	}
	
	public function in_menu(){
		$query;
		$query = mysql_query("SELECT id,title from pages WHERE in_menu = 1") or die (mysql_error);
		$menu = array();
		while ($result = mysql_fetch_array($query)){
			$menu[] = array('id'=>(int)$result['id'], 'title' => $result['title']);
		}
		return $menu;
	}
	
	public function mas_1($id){
		$res = array();	
		$query;
		$query_;
		$query = mysql_query("SELECT * FROM gamers WHERE userParent='".$id."'") or die (mysql_error);
		$query_ = mysql_num_rows($query);
		if ($query_){		
			$i = 0;
			while ($result = mysql_fetch_array($query)){
				$res[$i]= $result['UserChild'];
				$i++;
			}				
			return $res;
		}
		return false;
	}
	
	public function mas_2($result){
		$res = array();
		$result1 = array();
		$count = 0;
		//$result = $this->mas_1($id);
		$n = count($result);
		$j = 0;
		if ($n != 0){
			for ($i = 0; $i < $n; $i++){				
				$result1 = $this->mas_1($result[$i]);
				$count = count($result1);
				for ($k = 0; $k < $count; $k++){						
					$res[$j] = $result1[$k];
					$j++;
				}
			}			
			return $res;
		}
		return false;
	}
		
}
$admin_obj = new admin();
?>